package com.ymatou.iapi.optrade.testcase;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.CancelOrderTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.BatchAcceptOrderReq;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.BatchAcceptOrderCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.utils.constant.ECode;

/**
 * 批量接单 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_BatchAcceptOrder {

	private static BatchAcceptOrderReq batchAcceptOrderReq;
	private static BatchAcceptOrderCall batchacceptordercall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量接单");
	}

	@Before
	public void caseUp() {

		batchAcceptOrderReq = new BatchAcceptOrderReq();
		batchacceptordercall = new BatchAcceptOrderCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchacceptordercall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchAcceptOrder_001() {
		Logger.start(true, "批量接单成功");
		try {
			Logger.comment("前置数据准备");
			List<Long> list = new ArrayList<Long>();

			for (int i = 0; i < 2; i++) {

				TradingServiceResp placeOrderResp = TradingCallService
						.payOrder();

				OrderPo order = placeOrderResp.getOrderList().get(0)
						.getOrderPo();

				long orderId = order.getOrderId();

				long sellerId = order.getSellerId();

				list.add(orderId);

				batchAcceptOrderReq.setSellerId(sellerId);
			}

			Logger.comment("前置数据准备结束");
			// 参数
			batchAcceptOrderReq.setOrderIds(list);

			// 发送请求
			batchacceptordercall.setData(batchAcceptOrderReq);
			batchacceptordercall.callService();
			Logger.verifyEquals(ECode.SUCCESS, batchacceptordercall.getCode(),
					"验证Code");
			Logger.verifyEquals(true, batchacceptordercall
					.getAcceptOrderResultList().get(0).getIsSuccess(),
					"验证第一个订单接单状态");

			batchacceptordercall.getAcceptOrderResultList().forEach(item -> {
				Logger.verifyEquals(true, item.getIsSuccess(), "验证接单状态");
			});
			// 数据库验证
			list.forEach(orderId -> Ts_SellerAcceptOrder.verifyOrderStatus(
					orderId, batchAcceptOrderReq.getSellerId(), false));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchAcceptOrder_002() {
		Logger.start(true, "此订单不存在");
		try {
			List<Long> list = new ArrayList<Long>();
			list.add(123L);
			long sellerId = 288484;
			// 参数
			batchAcceptOrderReq.setOrderIds(list);
			batchAcceptOrderReq.setSellerId(sellerId);
			// 发送请求
			batchacceptordercall.setData(batchAcceptOrderReq);
			batchacceptordercall.callService();
			Logger.verifyEquals(ECode.SUCCESS, batchacceptordercall.getCode(),
					"验证Code");
			String info0 = batchacceptordercall.getAcceptOrderResultList()
					.get(0).getResultInfo();
			Logger.verifyEquals(true, info0.contains(ECode.FAILED201_MSG_11),
					"验证AcceptOrderResultList:ResultInfo包含字段："
							+ ECode.FAILED201_MSG_11);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchAcceptOrder_003() {
		Logger.start(true, "操作id为空");
		try {
			List<Long> list = new ArrayList<Long>();

			for (int i = 0; i < 2; i++) {

				TradingServiceResp placeOrderResp = TradingCallService
						.payOrder();

				OrderPo order = placeOrderResp.getOrderList().get(0)
						.getOrderPo();

				long orderId = order.getOrderId();

				list.add(orderId);

				//batchAcceptOrderReq.setSellerId(sellerId);
			}

			// 参数
			batchAcceptOrderReq.setOrderIds(list);
			//batchAcceptOrderReq.setSellerId(SellerId);
			// 发送请求
			batchacceptordercall.setData(batchAcceptOrderReq);
			batchacceptordercall.callService();
			Logger.verifyEquals(ECode.ERROR400, batchacceptordercall.getCode(),
					"验证Code");
			Logger.verifyEquals(ECode.BCODE8, batchacceptordercall.getBCode(),
					"验证Bcode");
			Logger.verifyEquals("sellerId:sellerId未提供",
					batchacceptordercall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchAcceptOrder_004() {
		Logger.start(true, "操作id跟实际接单买手不一致");
		try {
			Logger.comment("前置数据准备");
			List<Long> list = new ArrayList<Long>();

			for (int i = 0; i < 2; i++) {

				TradingServiceResp placeOrderResp = TradingCallService
						.payOrder();

				OrderPo order = placeOrderResp.getOrderList().get(0)
						.getOrderPo();

				long orderId = order.getOrderId();

				long sellerId = order.getSellerId();

				list.add(orderId);

				batchAcceptOrderReq.setSellerId(sellerId);
			}
			batchAcceptOrderReq.setOrderIds(list);
			batchAcceptOrderReq.setSellerId(11111L);
			// 发送请求
			batchacceptordercall.setData(batchAcceptOrderReq);
			batchacceptordercall.callService();

			Logger.verifyEquals(ECode.SUCCESS, batchacceptordercall.getCode(),
					"验证Code");
			Logger.verifyEquals(ECode.BCODE0, batchacceptordercall.getBCode(),
					"验证Bcode");
			//倒序
			Collections.reverse(list);

			for (int i = list.size(); i == 0; i--) {

				long orderId = list.get(i);

				Logger.verifyEquals(
						String.format("用户%s不是订单%s的所有者",
								batchAcceptOrderReq.getSellerId(), orderId),

						batchacceptordercall.getAcceptOrderResultList().get(i - 1)
								.getResultInfo(), "校验resultInforesultInfo");

			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchAcceptOrder_005() {
		Logger.start(true, "批量订单中有不能确认接单状态,批量接单返回结果正确");
		try {
			Logger.comment("前置数据准备");
			List<Long> list = new ArrayList<Long>();

			for (int i = 0; i < 2; i++) {

				TradingServiceResp placeOrderResp = TradingCallService
						.payOrder();

				OrderPo order = placeOrderResp.getOrderList().get(0)
						.getOrderPo();

				long orderId = order.getOrderId();

				long sellerId = order.getSellerId();

				if (i == 1) {

					TradingCallService.cancelOrder(orderId,
							CancelOrderTypeEnum.SELLER_CANCEL_ORDER, sellerId);

				}

				list.add(orderId);

				batchAcceptOrderReq.setSellerId(sellerId);
			}

			// 参数
			batchAcceptOrderReq.setOrderIds(list);
			// 发送请求
			batchacceptordercall.setData(batchAcceptOrderReq);
			batchacceptordercall.callService();
			Logger.verifyEquals(ECode.SUCCESS, batchacceptordercall.getCode(),
					"验证Code");

			Logger.verifyEquals(String.format("当前订单[%s]状态不能确认接单", list.get(1)),
					batchacceptordercall.getAcceptOrderResultList().get(0)
							.getResultInfo(), "校验resultInforesultInfo");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
